import { createRouter, createWebHistory } from "vue-router";
import NProgress from 'nprogress'; // progress bar
import 'nprogress/nprogress.css';
import { qiankunWindow } from 'vite-plugin-qiankun/dist/helper';
import config from '../../package.json';

const modules = import.meta.glob("./modules/*.js", { eager: true });
function formatModules(_modules, result) {
  Object.keys(_modules).forEach((key) => {
    const defaultModule = _modules[key].default;
    if (!defaultModule) return;
    const moduleList = Array.isArray(defaultModule)
      ? [...defaultModule]
      : [defaultModule];
    result.push(...moduleList);
  });
  return result;
}

const appRoutes = formatModules(modules, []);

NProgress.configure({ showSpinner: false });
// 路由参数配置
const router = createRouter({
  // 使用hash(createWebHashHistory)模式，(createWebHistory是HTML5历史模式，支持SEO)
  history: createWebHistory(qiankunWindow.__POWERED_BY_QIANKUN__ ? `/v3/${config.name}` : `/`),
  routes: [...appRoutes],
  scrollBehavior() {
    // 始终滚动到顶部
    return { top: 0 };
  },
});

// 导出默认值
export default router;
